package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseTransactionsCompanyDetailEntity;
import com.ly.heart.domain.dto.KingBaseTransactionsCompanyDto;
import com.ly.heart.domain.vo.KingBaseTransactionsCompanyVo;
import com.ly.heart.mapper.KingBaseTransactionsCompanyMapper;
import com.ly.heart.service.IKingBaseTransactionsCompanyDetailService;
import com.ly.heart.service.IKingBaseTransactionsCompanyService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseTransactionsCompanyEntity;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块-其他往来单位Service业务层处理
 *
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseTransactionsCompanyServiceImpl extends ServiceImpl<KingBaseTransactionsCompanyMapper, KingBaseTransactionsCompanyEntity> implements IKingBaseTransactionsCompanyService {

    @Resource
    private IKingBaseTransactionsCompanyDetailService kingBaseTransactionsCompanyDetailService;

    /**
     * 查询金蝶-财务模块-其他往来单位
     *
     * @param id 金蝶-财务模块-其他往来单位ID
     * @return 金蝶-财务模块-其他往来单位
     */
    @Override
    public KingBaseTransactionsCompanyVo selectKingBaseTransactionsCompanyEntityById ( String id ) {
        KingBaseTransactionsCompanyVo vo = new KingBaseTransactionsCompanyVo ();
        KingBaseTransactionsCompanyEntity transactionsCompanyEntity = this.getById ( id );
        List<KingBaseTransactionsCompanyDetailEntity> list = kingBaseTransactionsCompanyDetailService.list ( new LambdaQueryWrapper<KingBaseTransactionsCompanyDetailEntity> ().eq ( KingBaseTransactionsCompanyDetailEntity :: getTransactionsCompanyId, id ) );
        vo.setKingBaseTransactionsCompanyEntity ( transactionsCompanyEntity ).setKingBaseTransactionsCompanyDetailEntities ( list );
        return vo;
    }

    /**
     * 查询金蝶-财务模块-其他往来单位列表
     *
     * @param kingBaseTransactionsCompanyEntity 金蝶-财务模块-其他往来单位
     * @return 金蝶-财务模块-其他往来单位
     */
    @Override
    public Page selectKingBaseTransactionsCompanyEntityList ( KingBaseTransactionsCompanyEntity kingBaseTransactionsCompanyEntity ) {
        Page page = this.page ( new Page ( kingBaseTransactionsCompanyEntity.getPage (), kingBaseTransactionsCompanyEntity.getLimit () ), new LambdaQueryWrapper<KingBaseTransactionsCompanyEntity> ().eq ( KingBaseTransactionsCompanyEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        return page;
    }

    /**
     * 新增金蝶-财务模块-其他往来单位
     *
     * @param kingBaseTransactionsCompanyDto 金蝶-财务模块-其他往来单位
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseTransactionsCompanyEntity ( KingBaseTransactionsCompanyDto kingBaseTransactionsCompanyDto ) {
        KingBaseTransactionsCompanyEntity kingBaseTransactionsCompanyEntity = kingBaseTransactionsCompanyDto.getKingBaseTransactionsCompanyEntity ();
        kingBaseTransactionsCompanyEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBaseTransactionsCompanyEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTransactionsCompanyEntity.setCreateTime ( DateUtils.getNowDate () );
        updateTransactionsCompanyDetail ( kingBaseTransactionsCompanyDto.getKingBaseTransactionsCompanyDetailEntities (), kingBaseTransactionsCompanyEntity.getId () );
        boolean save = this.save ( kingBaseTransactionsCompanyEntity );
        if ( ! save ) {
            throw new CustomException ( "新增往来单位失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 修改金蝶-财务模块-其他往来单位
     *
     * @param kingBaseTransactionsCompanyDto 金蝶-财务模块-其他往来单位
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseTransactionsCompanyEntity ( KingBaseTransactionsCompanyDto kingBaseTransactionsCompanyDto ) {
        KingBaseTransactionsCompanyEntity kingBaseTransactionsCompanyEntity = kingBaseTransactionsCompanyDto.getKingBaseTransactionsCompanyEntity ();
        kingBaseTransactionsCompanyEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTransactionsCompanyEntity.setUpdateTime ( DateUtils.getNowDate () );
        updateTransactionsCompanyDetail ( kingBaseTransactionsCompanyDto.getKingBaseTransactionsCompanyDetailEntities (), kingBaseTransactionsCompanyEntity.getId () );
        boolean b = this.updateById ( kingBaseTransactionsCompanyEntity );
        if ( ! b ) {
            throw new CustomException ( "修改往来单位失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除金蝶-财务模块-其他往来单位
     *
     * @param ids 需要删除的金蝶-财务模块-其他往来单位ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseTransactionsCompanyEntityByIds ( String[] ids ) {
        List<KingBaseTransactionsCompanyEntity> list = this.list ( new LambdaQueryWrapper<KingBaseTransactionsCompanyEntity> ().in ( KingBaseTransactionsCompanyEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( company -> company.getIsDel () == 1 ).map ( KingBaseTransactionsCompanyEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( company -> {
            if ( company.getIsDel () == 2 ) {
                stringBuilder.append ( company.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTransactionsCompanyEntity> ().
                set ( KingBaseTransactionsCompanyEntity :: getIsUsed, ConstUtil.IS_USED_YES ).
                set ( KingBaseTransactionsCompanyEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTransactionsCompanyEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseTransactionsCompanyEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除其他往来单位失败！" );
        }
        String str = "删除其他往来单位成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-其他往来单位信息
     *
     * @param id 金蝶-财务模块-其他往来单位ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseTransactionsCompanyEntity transactionsCompanyEntity = this.getById ( id );
        if ( transactionsCompanyEntity == null ) {
            throw new CustomException ( "无此往来单位！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTransactionsCompanyEntity> ().
                set ( KingBaseTransactionsCompanyEntity :: getIsDisabled, transactionsCompanyEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseTransactionsCompanyEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTransactionsCompanyEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseTransactionsCompanyEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( transactionsCompanyEntity.getIsDisabled () == 1 ? "禁用往来单位失败！" : "解禁往来单位失败！" );
        }
        return AjaxResult.success ();
    }


    private void updateTransactionsCompanyDetail ( List<KingBaseTransactionsCompanyDetailEntity> kingBaseTransactionsCompanyDetailEntities, String id ) {
        if ( ! CollectionUtils.isEmpty ( kingBaseTransactionsCompanyDetailEntities ) ) {
            kingBaseTransactionsCompanyDetailService.remove ( new LambdaUpdateWrapper<KingBaseTransactionsCompanyDetailEntity> ().eq ( KingBaseTransactionsCompanyDetailEntity :: getTransactionsCompanyId, id ) );
            kingBaseTransactionsCompanyDetailEntities.forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setTransactionsCompanyId ( id );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = kingBaseTransactionsCompanyDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "更新其他往来单位财务信息失败！" );
                }
            } );
        }
    }

}
